package com.hsjk.szwj.module.train.controller.app;

import com.hsjk.szwj.framework.common.pojo.CommonResult;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.framework.security.core.annotations.PreAuthenticated;
import com.hsjk.szwj.framework.security.core.util.SecurityFrameworkUtils;
import com.hsjk.szwj.module.train.controller.app.vo.InitPersonalPaperReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.collect.AppMemberCourseCollectRecordCreateOrUpdateReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberActionRecordReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberCollectQuestionRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberCourseCollectQuestionReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberCourseErrorQuestionReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberErrorIdsReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberErrorQuestionRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberPaperListReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberPaperRecordPageReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberPaperRecordPageRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberPaperRecordReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberPaperRecordRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberPaperSummaryReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppMemberPaperSummaryRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppQuestionActionRecordReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppQuestionChapterListRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppQuestionListRespVO;
import com.hsjk.szwj.module.train.service.IMemberRecordService;
import com.hsjk.szwj.module.train.service.IQuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/08/20 17:47
 **/
@Tag(name = "应用端 - 题库相关接口")
@RestController
@RequestMapping("/train/question")
@Validated
public class AppQuestionController {

    @Resource
    private IQuestionService questionService;

    @Resource
    private IMemberRecordService memberRecordService;

    @PostMapping("/init-personal-paper")
    @Operation(summary = "初始化个性化考题")
    @PreAuthenticated
    public CommonResult<Boolean> initPersonalPaper(@RequestBody @Valid InitPersonalPaperReqVO reqVO) {
        questionService.initPersonalPaper(SecurityFrameworkUtils.getLoginUserId(), reqVO.getCarType(), reqVO.getSubject());
        return success(true);
    }

    @GetMapping("/list-id-by-set-id")
    @Operation(summary = "根据题集ID获得题目id列表")
    @PreAuthenticated
    public CommonResult<List<Long>> getQuestionIdListBySetId(@RequestParam("questionSetId") Long questionSetId) {
        return success(questionService.getQuestionIdListBySetId(questionSetId));
    }

    @GetMapping("/list-id-by-tag-id")
    @Operation(summary = "根据题目标签ID获得题目id列表")
    @PreAuthenticated
    public CommonResult<List<Long>> getQuestionIdListByTagId(@RequestParam("questionTagId") Long questionTagId) {
        return success(questionService.getQuestionIdListByTagId(questionTagId));
    }

    @PostMapping("/list-by-ids")
    @Operation(summary = "根据题目ID列表获得题目列表")
    @PreAuthenticated
    public CommonResult<List<AppQuestionListRespVO>> getQuestionListByIds(@RequestBody List<Long> ids) {
        return success(questionService.getQuestionListByIds(ids));
    }

    @PostMapping("/batch-create-member-action-record")
    @Operation(summary = "批量上传用户做题记录")
    @PreAuthenticated
    public CommonResult<Boolean> batchCreateMemberActionRecord(@RequestBody List<AppQuestionActionRecordReqVO> reqVOList) {
        return success(questionService.batchCreateMemberActionRecord(reqVOList));
    }

    @PostMapping("/list-member-action-record")
    @Operation(summary = "获取当前用户的所有做题记录")
    @PreAuthenticated
    public CommonResult<List<AppQuestionActionRecordReqVO>> getMemberActionRecord(@RequestBody AppMemberActionRecordReqVO reqVO) {
        return success(questionService.getMemberActionRecord(reqVO));
    }

    @PostMapping("/create-paper-record")
    @Operation(summary = "创建用户做试卷记录")
    @PreAuthenticated
    public CommonResult<Boolean> createPaperRecord(@Valid @RequestBody AppMemberPaperRecordReqVO reqVO) {
        return success(questionService.createMemberPaperRecord(reqVO));
    }

    @PostMapping("/list-member-paper-ids")
    @Operation(summary = "获取当前用户的题目id列表")
    @PreAuthenticated
    public CommonResult<List<Long>> getCurrentMemberQuestionIdList(@Valid @RequestBody AppMemberPaperListReqVO reqVO) {
        return success(questionService.getCurrentMemberQuestionIdList(reqVO));
    }

    @GetMapping("/get-paper-record-by-paper-id")
    @Operation(summary = "根据试卷ID获取用户做试卷记录")
    @PreAuthenticated
    public CommonResult<AppMemberPaperRecordRespVO> getPaperRecordByPaperId(@RequestParam("paperId") Long paperId) {
        return success(questionService.getPaperRecordByPaperId(paperId));
    }

    @PostMapping("/list-member-error-ids")
    @Operation(summary = "获取当前用户错题Id集合")
    @PreAuthenticated
    public CommonResult<List<Long>> getCurrentMemberErrorQuestionIdList(@RequestBody @Valid AppMemberErrorIdsReqVO reqVO) {
        return success(questionService.getCurrentMemberErrorQuestionIdList(reqVO));
    }

//    @PostMapping("/list-member-collect-ids")
//    @Operation(summary = "获取当前用户收藏题目ID集合")
//    @PreAuthenticated
//    public CommonResult<List<Long>> getCurrentMemberCollectQuestionIdList(@RequestBody @Valid AppMemberCollectIdsReqVO reqVO) {
//        return success(questionService.getCurrentMemberCollectQuestionIdList(reqVO));
//    }

    @PostMapping("/create-or-update-member-course-collect")
    @Operation(summary = "创建/修改用户课程收藏")
    @PreAuthenticated
    public CommonResult<Boolean> createOrUpdateMemberCollectCourseRecord(@RequestBody @Valid AppMemberCourseCollectRecordCreateOrUpdateReqVO reqVO) {
        return success(memberRecordService.createOrUpdateMemberCollectCourseRecord(reqVO));
    }

    @PostMapping("/list-chapter-by-ids")
    @Operation(summary = "根据章节ID列表获取章节列表")
    @PreAuthenticated
    public CommonResult<List<AppQuestionChapterListRespVO>> getChapterListByIds(@RequestBody List<Long> ids) {
        return success(questionService.getChapterListByIds(ids));
    }

    @PostMapping("/list-chapter-by-question-ids")
    @Operation(summary = "根据题目ID列表获取章节列表")
    @PreAuthenticated
    public CommonResult<List<AppQuestionChapterListRespVO>> getChapterListByQuestionIds(@RequestBody List<Long> ids) {
        return success(questionService.getChapterListByQuestionIds(ids));
    }

    @PostMapping("/get-member-paper-summary")
    @Operation(summary = "获取当前用户模考汇总信息")
    @PreAuthenticated
    public CommonResult<AppMemberPaperSummaryRespVO> getMemberPaperSummary(@RequestBody AppMemberPaperSummaryReqVO reqVO) {
        return success(questionService.getMemberPaperSummary(reqVO));
    }

    @PostMapping("/page-member-paper-record")
    @Operation(summary = "分页查询当前用户模考信息")
    @PreAuthenticated
    public CommonResult<PageResult<AppMemberPaperRecordPageRespVO>> getMemberPaperRecordPage(@RequestBody @Valid AppMemberPaperRecordPageReqVO reqVO) {
        return success(questionService.getMemberPaperRecordPage(reqVO));
    }

    @GetMapping("/list-member-error-chapter-by-course-id")
    @Operation(summary = "根据课程id获取该课程下全部错题的章节")
    @PreAuthenticated
    public CommonResult<List<AppMemberErrorQuestionRespVO>> getMemberErrorChapterByCourseId(@RequestParam Long courseId) {
        return success(memberRecordService.getMemberChapterErrorQuestionByCourseId(courseId));
    }

    @GetMapping("/list-member-collect-chapter-by-course-id")
    @Operation(summary = "根据课程id获取该课程下全部收藏的章节")
    @PreAuthenticated
    public CommonResult<List<AppMemberCollectQuestionRespVO>> getMemberChapterCollectQuestionByCourseId(@RequestParam Long courseId) {
        return success(memberRecordService.getMemberChapterCollectQuestionByCourseId(courseId));
    }

    @PostMapping("/list-member-course-error-question-ids")
    @Operation(summary = "获取课程下指定章节的错题id集合")
    @PreAuthenticated
    public CommonResult<List<Long>> getMemberCourseErrorQuestionIdList(@RequestBody AppMemberCourseErrorQuestionReqVO reqVO) {
        return success(memberRecordService.getMemberCourseErrorQuestionIdList(reqVO));
    }

    @PostMapping("/list-member-course-collect-question-ids")
    @Operation(summary = "获取课程下指定章节的收藏id集合")
    @PreAuthenticated
    public CommonResult<List<Long>> getMemberCourseCollectQuestionIdList(@RequestBody AppMemberCourseCollectQuestionReqVO reqVO) {
        return success(memberRecordService.getMemberCourseCollectQuestionIdList(reqVO));
    }

    @GetMapping("/list-member-collect-ids-by-course-id")
    @Operation(summary = "获取当前用户某课程下收藏题目ID集合")
    @PreAuthenticated
    public CommonResult<List<Long>> getCurrentMemberCollectQuestionIdList(@RequestParam Long courseId) {
        return success(questionService.getCurrentMemberCollectQuestionIdList(courseId));
    }
}
